package com.example.onlinemonitoring.Util;

import com.example.onlinemonitoring.Util.language.CH;
import com.example.onlinemonitoring.Util.language.EN;
import com.example.onlinemonitoring.Util.language.ES;
import com.example.onlinemonitoring.Util.language.RU;
import com.example.onlinemonitoring.Util.language._CurrentLanguage;
import com.example.onlinemonitoring.autoRun.ComRunner;
import com.example.onlinemonitoring.comCenter.JdqCom;
import com.example.onlinemonitoring.serviceImpl.AppdeviceServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppdeviceTempServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppstationServiceImpl;
import com.example.onlinemonitoring.serviceImpl.OilsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.YggxsensorServiceImpl;
import com.example.onlinemonitoring.model.Appstation;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import lombok.Data;

@Data
@Service
public class LanguageManager extends HexUtil {
    @Autowired
    private AppstationServiceImpl appstationserviceimpl;
    @Autowired
    private OilsensorServiceImpl oilserviceimpl;
    @Autowired
    private AppdeviceServiceImpl appdeviceserviceimpl;
    @Autowired
    private AppdeviceTempServiceImpl appdevicetempserviceimpl;
    @Autowired
    private YggxsensorServiceImpl yggxsensorServiceImpl;
    @Autowired
    private ApplicationContext context; // 自动装配ApplicationContext对象

    private Integer MyLanguage = 0;
    private _CurrentLanguage lan = new CH();

    @PostConstruct
    public void init() {
        myInit(getLanguage());
    }

    public void myInit(int Language) {
        switch (Language) {
            case 0:
                lan = new CH();
                break;// 中文
            case 1:
                lan = new EN();
                break;// 英文
            case 2:
                lan = new ES();
                break;// 西班牙
            case 3:
                lan = new RU();
                break;// 俄语
            default:
                break;
        }
    }

    public void changeDeviceLanguage() {
        // 智能变电站在线监测系统标题切换，每次新增一种语言这里必须新增一个判断，防止客户修改了又被改回来
        Appstation as = new Appstation();
        as.setId(1);
        as = appstationserviceimpl.selectByPrimaryKey(as);
        String longName = as.getLongname();
        boolean isTitleMatched = longName.equals(new CH().getTitle()) || longName.equals(new EN().getTitle())
                || longName.equals(new ES().getTitle()) || longName.equals(new RU().getTitle());
        if (isTitleMatched) {
            appstationserviceimpl.UpdateTitle(lan.getTitle());
        }

        // 修改sensor表中的style字段
        oilserviceimpl.UpdateStyle(lan.getStyleOil());
        yggxsensorServiceImpl.UpdateStyle(lan.getStyleOptical());
        // 油色谱
        appdevicetempserviceimpl.UpdateAppdeviceTempName(lan.getNameOil(), "oil");// 修改appdevice_temp数据库中的名称
        appdeviceserviceimpl.UpdateAppdeviceName(lan.getNameOil(), "oil");// 修改appdevice数据库中的名称
        UpdateRunningAppdeviceName(lan.getNameOil(), "oil");

        // 荧光光纤
        appdevicetempserviceimpl.UpdateAppdeviceTempName(lan.getNameOptical(), "yggx");
        appdeviceserviceimpl.UpdateAppdeviceName(lan.getNameOptical(), "yggx");
        UpdateRunningAppdeviceName(lan.getNameOptical(), "yggx");

        // 继电器
        appdevicetempserviceimpl.UpdateAppdeviceTempName(lan.getNameJdq(), "jdq");// 修改appdevice_temp数据库中的名称
        appdeviceserviceimpl.UpdateAppdeviceName(lan.getNameJdq(), "jdq");// 修改appdevice数据库中的名称
        UpdateRunningAppdeviceName(lan.getNameJdq(), "jdq");
    }

    public int getLanguage() {
        Appstation as = new Appstation();
        as.setId(1);
        as = appstationserviceimpl.selectByPrimaryKey(as);
        MyLanguage = as.getLanguage();

        return MyLanguage;
    }

    public void UpdateRunningAppdeviceName(String name, String simple) {
        for (Map.Entry<Integer, CommPortManager> entry : ComRunner.devmap.entrySet()) {
            CommPortManager value = entry.getValue();
            value.reloadAppdevice();
        }
        for (Map.Entry<Integer, CommPortManager> entry : ComRunner.netmap.entrySet()) {
            CommPortManager value = entry.getValue();
            value.reloadAppdevice();
        }
        for (Map.Entry<Integer, CommPortManager> entry : ComRunner.netservermap.entrySet()) {
            CommPortManager value = entry.getValue();
            value.reloadAppdevice();
        }
    }
}
